import time 
from random import randint 
from elasticsearch import Elasticsearch, helpers 

es_server = ["127.0.0.1:9200"] 
index_name = 'demo' 
type_name = 'f5' 
es = Elasticsearch(es_server) 

mapping = { 
   "timestamp": { 
      "type": "date", 
      "format":"yyyy-MM-dd'T'HH:mm:ssZ", 
      "index": "not_analyzed", 
      "store": False 
   }, 
   "cpu": { 
      "type": "integer", 
       "store": False 
   }, 
   "memory": { 
      "type": "integer", 
       "store": False 
   }, 
   "session": { 
      "type": "integer", 
       "store": False 
   }, 
   "hostname": { 
      "type": "string", 
      "index": "not_analyzed", 
      "store": False 
   } 
} 
try: 
    es.indices.delete(index_name) 
except: 
    pass 
es.indices.create(index_name) 
es.cluster.health(wait_for_status="yellow") 
res = es.indices.put_mapping(index=index_name, 
                             doc_type=type_name, 
                             body={type_name: {"properties": mapping}}) 

hostnames = ['DEVICE%02d' % i for i in range(1, 11)] 
now = int(time.time()) 
start = now - 3600 * 24 * 2 
end = now + 3600 * 24 * 8 
size = 1000 
n = 0 
actions = [] 

for i in range(start, end, 10): 
    for h in hostnames: 
        doc = dict(hostname=h, 
                   timestamp=time.strftime('%Y-%m-%dT%H:%M:%SZ', time.localtime(i)), 
                   cpu=randint(0,80), 
                   memory=randint(1000,2000) * 10000, 
                   session=randint(1000,10000)) 
        action = { 
            "_index": index_name, 
            "_type": type_name, 
            "_source": doc 
            } 
        actions.append(action) 
    n += 1 
    if n >= 1000:             
        n = 0 
        helpers.bulk(es, actions) 
        actions = [] 

if actions: 
    helpers.bulk(es, actions) 

print(time.time() - now)